AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:CreateTimers()
	timer.Create(tostring(self) .. "payday", self.PayDayTime, 0, self.PayDay, self)
	timer.Create(tostring(self) .. "makedrug", self.DrugTime, 1, self.MakeDrug, self)
	timer.Create(tostring(self) .. "warnturnoff", self.TurnOffTime - (self.TurnOffTime * 0.1), 1, self.WarnTurnOff, self)
	timer.Create(tostring(self) .. "turnoff", self.TurnOffTime, 1, self.TurnOff, self)
end

function ENT:Use(activator, caller)
	if !self.World && !self.dt.Sparking then self:CreateTimers() end
	self.dt.Sparking = true
	self.Active = true
	self:SetColor(255, 255, 255, 255)
end

function ENT:MakeDrug()
	local spos = self.SparkPos
	local ang = self:GetAngles()

	drug = ents.Create("drug_random")
	drug:SetPos(self:GetPos() + ang:Forward() * spos.x + ang:Right() * spos.y + ang:Up() * spos.z)
	drug:Spawn()
	
	self.dt.Sparking = false
	if  math.random(1, self.BlowUpChance) == math.random(1, self.BlowUpChance) then
		self:Explode()
		util.BlastDamage(self.dt.Owner, self.dt.Owner, self:GetPos(), 384, 175)
		Notify(self.dt.Owner, 4, 3, "A METH LAB HAS BLOWN RIGHT THE FUCK UP!")
	end
end

function ENT:PayDay()
	if(self.World) then return end
	self.BaseClass.PayDay(self)
	if  math.random(1, self.BlowUpChance) == math.random(1, self.BlowUpChance) then
		self:Explode()
		util.BlastDamage(self.dt.Owner, self.dt.Owner, self:GetPos(), 384, 175)
		Notify(self.dt.Owner, 4, 3, "A METH LAB HAS BLOWN RIGHT THE FUCK UP!")
	end
end

function ENT:OnRemove()
	timer.Remove(tostring(self) .. "payday")
	timer.Remove(tostring(self) .. "makedrug")
	timer.Remove(tostring(self) .. "warnturnoff")
	timer.Remove(tostring(self) .. "turnoff")
end